import 'dart:io';

import 'package:aio/aio.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:app/generated/app_strings.dart';
import 'package:app/src/http/bean/wroks/attachment_bean.dart';
import 'package:app/src/http/bean/wroks/contract_bean.dart';
import 'package:app/src/http/bean/wroks/customers_copyInfo.dart';
import 'package:app/src/http/model/user_model.dart';
import 'package:app/src/ui/app_ui.dart';
import 'package:app/src/ui/dialog/dailog.dart';
import 'package:app/src/ui/route/helper/route_helper.dart';
import 'package:app/src/ui/route/works/public/items/ui_base.dart';
import 'package:app/src/ui/route/works/public/items/ui_card.dart';
import 'package:app/src/utils/image_path_utils.dart';

///图片上传
class UiImageUpLoad {
  //本地图片
  static List<File>? files = null;
  static String mId = "";

  //图片清除
  ///在 ReviewDetailsUi 返回键监听里，有调用
  static void clear() {
    files?.clear();
    files = null;
  }

  //移除指定下标的图片
  static removeAt(int index) {
    if (files != null && files!.length > index) {
      files?.removeAt(index);
    }
  }

  static double _width = 100; //图片宽
  static double _height = 60; //图片高

  ///title 标题
  static Widget build(BuildContext context, String? id, {String? title}) {
    if (StringUtils.isEmpty(title)) {
      title = AppStrings.of(context).UploadEvidence; //上传证据
    }
    if (mId != id) {
      clear();
    }
    mId = id ?? "";
    if (files == null) {
      files = [];
    }
    List<Widget> children = [];
    children.add(UiBase.title(context, title, bottom: 8));
    children.add(_images(context));
    var column = UiBase.column(context, children);
    var card = UiCard.build(context, column);
    return card;
  }

  static Widget _images(BuildContext context) {
    return StatefulBuilder(
        builder: (BuildContext context, StateSetter setState) {
      if (files == null) {
        files = [];
      }
      var listView = ListView.builder(
        scrollDirection: Axis.horizontal,
        //Axis.horizontal水平方向;Axis.vertical垂直方向（默认）
        //mainAxisExtent主轴方向元素的固定高度。
        itemCount: files!.length + 1,
        itemBuilder: (context, index) {
          Widget? child = null;
          if (index == 0) {
            var image = Image.asset(
              ImagePathUtils.getPathWithImageAdd("ic_add.png"),
              width: 50,
            );
            child = InkWell(
              child: Center(
                child: image,
              ),
              onTap: () {
                //图片选择
                chooseImage(context, () {
                  setState(() {});
                });
              },
            );
            return Container(
              child: child,
              margin: EdgeInsets.only(right: 6),
            );
          } else {
            var position = index - 1;
            var file = files![position];
            Widget image = Image.file(
              file,
              width: _width,
              height: _height,
              fit: BoxFit.cover,
            );
            child = InkWell(
              child: image,
              onTap: () {
                //图片预览
                AppRouteHelper.goPreviewImagesRoute(
                    context, FileUtils.getFilePathsWithFiles(files),
                    index: position);
              },
            );
            var del = Positioned(
              child: InkWell(
                child: Container(
                  width: 20,
                  height: 20,
                  child: Icon(
                    Icons.clear,
                    size: 20,
                    color: Colors.white,
                  ),
                  decoration: BoxDecoration(
                      color: Colors.redAccent, shape: BoxShape.circle),
                ),
                onTap: () {
                  //删除
                  AppDialog.showDialogWithDel(context, response: (value) {
                    if (value) {
                      setState(() {
                        removeAt(position);
                      });
                    }
                  });
                },
              ),
              left: _width - 10,
            );
            return Container(
              width: _width + 10,
              child: Stack(
                children: [
                  Positioned(
                    child: child,
                    top: 5,
                  ),
                  del
                ],
              ),
              margin: EdgeInsets.only(right: 6),
            );
          }
        },
        physics: BouncingScrollPhysics(),
      );
      return Container(
        child: listView,
        height: _height + 5,
      );
    });
  }

  ///图片选择
  static void chooseImage(
      BuildContext context, ResponseCallbackWithVoid? callbackWithVoid) {
    AppDialog.showDialogWithPickerPhoto(context, (value) {
      if (value?.path != null) {
        if (files == null) {
          files = [];
        }
        files?.insert(0, File(value.path)); //新图片，插入到第一个。
        callbackWithVoid?.call();
      }
    }, headPath: null);
  }

  ///图片上传
  static void syspicupload(
      BuildContext context, ResponseCallbackWithString_? callback) {
    if (StringUtils.isEmpty(files)) {
      callback?.call(null);
    } else {
      UserModel.syspicupload(context, true, files!, (value) {
        var pics = value?.Data; //图片地址
        callback?.call(pics);
      });
    }
  }
}
